package consul.allinone.discovery;

import consul.allinone.RpcURL;

import java.util.concurrent.CopyOnWriteArrayList;

public class RpcClientInvokerCache {

    private static CopyOnWriteArrayList<RpcURL> connectedHandlers = new CopyOnWriteArrayList<>();

    public static CopyOnWriteArrayList<RpcURL> getConnectedHandlersClone() {
        return (CopyOnWriteArrayList<RpcURL>) getConnectedHandlers().clone();
    }

    public static void addHandler(RpcURL handler) {
        CopyOnWriteArrayList<RpcURL> newHandlers = getConnectedHandlersClone();
        newHandlers.add(handler);
        connectedHandlers = newHandlers;
    }

    public static CopyOnWriteArrayList<RpcURL> getConnectedHandlers() {
        return connectedHandlers;
    }

    public static RpcURL get(int i) {
        return connectedHandlers.get(i);
    }

    public static int size() {
        return connectedHandlers.size();
    }

    public static void clear() {
        CopyOnWriteArrayList<RpcURL> newHandlers = getConnectedHandlersClone();
        newHandlers.clear();
        connectedHandlers = newHandlers;
    }
}